package com.retailcloud.baseservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.retailcloud.baseservice.dao.UsersDao;
import com.retailcloud.baseservice.entity.UsersEntity;
import com.retailcloud.baseservice.feign.ThirdPartyFeignService;
import com.retailcloud.baseservice.vo.TenantsVo;
import com.retailcloud.commonutils.constant.TenantStatusEnum;
import com.retailcloud.commonutils.constant.TenantsChargeStatusEnum;
import com.retailcloud.commonutils.exception.RetailCloudException;
import com.retailcloud.commonutils.utils.PageUtils;
import com.retailcloud.commonutils.utils.Query;
import com.retailcloud.commonutils.utils.R;
import com.retailcloud.commonutils.utils.RedisUtils;
import com.sitech.idworkstarter.IdWorkService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.retailcloud.baseservice.dao.TenantsDao;
import com.retailcloud.baseservice.entity.TenantsEntity;
import com.retailcloud.baseservice.service.TenantsService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Service("tenantsService")
public class TenantsServiceImpl extends ServiceImpl<TenantsDao, TenantsEntity> implements TenantsService {

    @Resource
    IdWorkService idWorkService;
    @Resource
    UsersDao usersDao;
    @Resource
    ThirdPartyFeignService thirdPartyFeignService;
    @Resource
    RedisUtils redisUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TenantsEntity> page = this.page(
                new Query<TenantsEntity>().getPage(params),
                new QueryWrapper<TenantsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveTenants(TenantsVo tenants, HttpServletRequest request) {
        if (StringUtils.isNotBlank(tenants.getContactPhone())) {
            //验证短信验证码
            String code = redisUtils.get("smsCode:" + tenants.getContactPhone());
            if(StringUtils.isBlank(code)) {
                throw new RetailCloudException("短信验证码不正确或已过期", 5001);
            }
            if (!code.equals(tenants.getCode())) {
                throw new RetailCloudException("短信验证码不正确或已过期", 5001);
            }
        }
        //获取用户id
        String userId = request.getHeader("X-User-Name");
        tenants.setCreatedBy(userId);
        tenants.setUpdatedBy(userId);
        tenants.setTenantId(idWorkService.getSEQByKey("id_seq"));
        tenants.setChargeStatus(TenantsChargeStatusEnum.未付费);
        tenants.setStatus(TenantStatusEnum.待审核);
        baseMapper.insert(tenants);
        //更新用户表的企业信息
        UpdateWrapper<UsersEntity> usersEntityUpdateWrapper = new UpdateWrapper<>();
        usersEntityUpdateWrapper.eq("wechat_openid", userId);
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setTenantId(tenants.getTenantId());
        usersEntity.setUpdatedAt(tenants.getUpdatedAt());
        usersDao.update(usersEntity, usersEntityUpdateWrapper);

        Map<String, String> map = new HashMap<>();
        map.put("phoneNumber", "13852147293");
        //发短信告知审核
        thirdPartyFeignService.SendMessage(map);

    }

}
